import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:hao_ke/consts/consts.dart';
import 'package:hao_ke/pages/auth/auth.dart';
import 'package:hao_ke/pages/index/index_info_item.dart';
import 'package:hao_ke/pages/utils/scoped_model_helper.dart';
import 'package:hao_ke/utils/dio_http.dart';
import 'package:hao_ke/widgets/search_bar_widget.dart';

import '../../consts/url_const.dart';

class InfoPage extends StatefulWidget {
  const InfoPage({super.key});



  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _InfoPageState();
  }

}

class _InfoPageState extends State<InfoPage> {

  List<Widget> list = [];

  ScrollController _scrollController = ScrollController();

  int currentIndex = 1;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: SearchBarWidget(searchFunc: (value) {
          setState(() {
            currentIndex = 1;
          });
          getProjectList( params: {
            "name": value
          });
        },),
        centerTitle: true,
      ),
      body: RefreshIndicator(
        onRefresh: _refreshData,
        child: Container(
          child: Container(
            decoration: BoxDecoration(
                gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                      Color.fromRGBO(200, 100, 100, 0.5),
                      Color.fromRGBO(0, 0, 200, 0.5),
                    ]
                )
            ),
            child: ListView(
              scrollDirection: Axis.vertical,
              controller: _scrollController,
              children: list,
            ),
          ),
        )
      ),
    );
  }

  Future<void> _refreshData() async {
    currentIndex = 1;
    getProjectList();
  }

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
        getProjectList();
      }
    });
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getProjectList();
    });
  }





  Future<void> getProjectList ({Map<String, Object>? params}) async {
    print("页数：${currentIndex}");
    if(currentIndex==1) {
      setState(() {
        list = [];
      });
    }
    if(params ==null || params['name'] == "") {
      params = {
        'current': currentIndex,
        'size': 20,
      };
    }
    var response = await DioHttp().init(context, ScopedModelHelper.getModel<AuthModel>(context).token).get(UrlConst.projectList,params: params);
    List<Widget> res = [];

    for(var project in response['records']){
      res.add(IndexInfoItem(project['name'], project['remark'],project['price']==null?'':project['price'].toString(), project['image'], project['pkid']));
      res.add(SizedBox(height: 1,));
    }
    if(res.length<10) {
      res.add(Container( height: 60,alignment: Alignment.center, child: Text('没有更多数据了')));
    }
    if(currentIndex==1) {
      setState(() {
        list = res;
      });
    }else{
      setState(() {
        list.addAll(res);
      });
    }
    currentIndex = currentIndex + 1;
  }
}
